package com.info33.platform.business.app.controller;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.info33.platform.business.app.dto.AppTenantMenuDTO;
import com.info33.platform.business.app.entity.AppMenu;
import com.info33.platform.business.app.entity.AppTenantMenu;
import com.info33.platform.business.app.service.AppTenantMenuService;
import com.info33.platform.business.app.vo.AppHomeVO;
import com.info33.platform.business.app.vo.AppTenantMenuVO;
import com.info33.platform.common.api.Apis;
import com.info33.platform.common.mvc.controller.BaseController;
import com.info33.platform.common.mvc.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Point;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author admin
 */
@RestController
@RequestMapping("/AppTenantMenu")
@Api(tags = "app - 租户 - 菜单")
public class AppTenantMenuController extends BaseController<AppTenantMenuService, AppTenantMenu> {

    private final AppTenantMenuService appTenantMenuService;
    @Autowired
    private Apis apis;

    @Autowired
    public AppTenantMenuController(AppTenantMenuService appTenantMenuService) {
        super(appTenantMenuService);
        this.appTenantMenuService = appTenantMenuService;
    }

    /*
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     *
     * app
     *
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     */

    @GetMapping(value = "/app/home")
    @ApiOperation(value = "app - 首页")
    public Result<AppHomeVO> appHome() {
        return Result.ok(appTenantMenuService.appHome(getCurrentUser()));
    }

    @PostMapping(value = "/app/home/weather")
    @ApiOperation(value = "app - 首页 - 天气")
    public Result<JSONObject> appHomeWeather(
            @RequestBody JSONObject jsonObject) {
        return Result.ok(apis.getWeather(jsonObject.getStr("city")));
    }

    @GetMapping(value = "/app/home/weather/{code}")
    @ApiOperation(value = "app - 首页 - 天气")
    public Result<JSONObject> appHomeWeather(
            @PathVariable String code) {
        return Result.ok(apis.getWeatherByCode(code));
    }

    @PostMapping(value = "/app/address")
    @ApiOperation(value = "app 获取当前经纬度地址")
    public Result<String> appAddress(
            @RequestBody Point point) {
        return apis.getAddressByPoint(point);
    }

    @GetMapping(value = "/app/more")
    @ApiOperation(value = "app - 首页 - 菜单 - 更多")
    public Result<AppTenantMenuVO> appMore() {
        return Result.ok(appTenantMenuService.appMore(getCurrentUser()));
    }

    @GetMapping(value = "/app/home/menu")
    @ApiOperation(value = "app - 首页内容菜单")
    public Result<List<AppMenu>> appHomeMenu() {
        return Result.ok(appTenantMenuService.appHomeMenu(getCurrentUser()));
    }

    @GetMapping(value = "/app/my/menu")
    @ApiOperation(value = "app - 我所拥有的菜单")
    public Result<List<AppMenu>> appMyMenu() {
        return Result.ok(appTenantMenuService.appHomeMenuToSort(getCurrentUser()));
    }

    /*
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     *
     * pc
     *
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     */

    @PostMapping(value = "/pc/page/{pageNum}/{pageSize}")
    @ApiOperation(value = "pc - 菜单分页", notes = "入参说明：<br/>" +
            "menuDTO.pid = 1：顶级菜单<br/>" +
            "menuDTO.pid = ?：二级菜单（menuId 取值）")
    public Result<IPage<AppTenantMenuVO>> pcPage(
            @RequestBody AppTenantMenuDTO dto,
            @PathVariable int pageNum,
            @PathVariable int pageSize) {
        return Result.ok(appTenantMenuService.pcPage(new Page<>(pageNum, pageSize), dto, getCurrentUser()));
    }

    @GetMapping(value = "/pc/detail/{id}")
    @ApiOperation(value = "pc - 菜单详情", notes = "权限详情")
    public Result<AppTenantMenuVO> pcDetail(
            @PathVariable(value = "id") Long id) {
        return Result.ok(appTenantMenuService.pcDetail(id));
    }

    @PatchMapping(value = "/pc/reset/{id}")
    @ApiOperation(value = "pc - 重置")
    public Result<Object> pcReset(
            @PathVariable(value = "id") Long id) {
        return appTenantMenuService.pcReset(id);
    }

    @PutMapping(value = "/pc/edit")
    @ApiOperation(value = "pc - 编辑")
    public Result<Object> pcEdit(
            @RequestBody AppTenantMenuDTO dto) {
        return appTenantMenuService.pcEdit(dto);
    }

    @GetMapping(value = "/pc/tree")
    @ApiOperation(value = "pc - 树结构")
    public Result<List<AppTenantMenuVO>> pcTree() {
        return Result.ok(appTenantMenuService.pcTree(getTenantId()));
    }

}